﻿using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.Xml.Linq;
//
using PerceptiveMCAPI.Types.Internal;
//
namespace PerceptiveMCAPI.Types
{
   public class listGrowthHistoryOutput : Api_BaseOutput
   {
      public List<listGrowthHistoryResults> result { get; set; }
      public listGrowthHistoryParms inputParms { get; private set; }
      // ------------------------------------------------------------
      public listGrowthHistoryOutput( listGrowthHistoryInput input )
         : base( input )
      {
         inputParms = input.parms;
         result = new List<listGrowthHistoryResults>();
      }
      // --------------------------------------------------- tostring
      public override string ToString()
      {
         return string.Format( "Growth count: {0}", result.Count );
      }

      #region ================================================================== format setup

      // ---- serial
      internal void Format( string api_Request, string api_Response )
      {
         this.api_Request = api_Request;
         this.api_Response = api_Response;
         // ----------------------------------
         if ( this.api_OutputType == EnumValues.OutputType.JSON )
            outputJSON();
         else if ( this.api_OutputType == EnumValues.OutputType.XML )
            outputXML();
      }

      // ---- xmlrpc
      internal void Format( x_listGrowthHistory[] rpcResult )
      {
         this.api_Request = EnumValues.NotSupplied;
         this.api_Response = EnumValues.NotSupplied;
         // ------------------------
         this.result = FormatResults( rpcResult );
      }

      #endregion

      #region ================================================================= Output

      // ---------------------------------------------------------- outputXML
      private void outputXML()
      {
         XElement xml = XElement.Parse( this.api_Response );
         // error check
         Api_Error error = apiSerial.GetXmlOutputError( xml );
         if ( error != null )
         {
            this.api_ErrorMessages.Add( error );
            return;
         }
         // -------------------------------------------- get values
         try
         {
            int i;
            IEnumerable<XElement> months = xml.Elements( "struct" );
            foreach ( XElement month in months )
            {
               listGrowthHistoryResults hist = new listGrowthHistoryResults();
               int.TryParse( month.Element( "month" ).Value.Substring( 0, 4 ), out i );
               hist.year = i;
               int.TryParse( month.Element( "month" ).Value.Substring( 5, 2 ), out i );
               hist.month = i;
               int.TryParse( month.Element( "existing" ).Value, out i );
               hist.existing = i;
               int.TryParse( month.Element( "imports" ).Value, out i );
               hist.imports = i;
               int.TryParse( month.Element( "optins" ).Value, out i );
               hist.optins = i;
               this.result.Add( hist );
            }
         }
         catch ( Exception ex )
         {
            this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
         }
      }

      // ---------------------------------------------------------- outputJSON
      private void outputJSON()
      {
         JavaScriptSerializer jss = new JavaScriptSerializer();
         try
         {
            if ( this.api_Response.Contains( EnumValues.JSONErrorString ) )
            {
               Api_Error err = jss.Deserialize<Api_Error>( this.api_Response );
               this.api_ErrorMessages.Add( err );
            }
            else
            {
               x_listGrowthHistory[] jsonResult = jss.Deserialize<x_listGrowthHistory[]>( this.api_Response );
               this.result = FormatResults( jsonResult );
            }
         }
         catch ( Exception ex )
         {
            this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
         }
      }

      // ---------------------------------------------------------- Format Rpc & JSON
      private List<listGrowthHistoryResults> FormatResults( x_listGrowthHistory[] x_results )
      {
         List<listGrowthHistoryResults> results = new List<listGrowthHistoryResults>();
         int i;
         foreach ( x_listGrowthHistory result in x_results )
         {
            listGrowthHistoryResults hist = new listGrowthHistoryResults();
            int.TryParse( result.month.Substring( 0, 4 ), out i );
            hist.year = i;
            int.TryParse( result.month.Substring( 5, 2 ), out i );
            hist.month = i;
            int.TryParse( result.existing, out i );
            hist.existing = i;
            int.TryParse( result.imports, out i );
            hist.imports = i;
            int.TryParse( result.optins, out i );
            hist.optins = i;
            results.Add( hist );
         }
         return results;
      }

      #endregion
   }
}
